package logic;

import dao.UserDAO;
import dto.UserDTO;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.InputHelper;

/**
 *
 * @author lucifer
 */
public class User {

  public static final int REGULAR = 1;
  public static final int MEDICO = 2;

  private int id;
  private int gid;
  private int idMedico;
  private String username;
  private String password;

  public User(int id, int gid, int idMedico, String username, String password) {
    this.id = id;
    this.gid = gid;
    this.idMedico = idMedico;
    this.username = username;
    this.password = InputHelper.SHA512(password);
  }

  public User(UserDTO dto) {
    this.id = dto.getId();
    this.gid = dto.getGid();
    this.idMedico = dto.getIdMedico();
    this.username = dto.getUsername();
    this.password = dto.getPassword();
  }

  public void Save() throws SQLException {
    UserDTO dto = new UserDTO(-1, gid, idMedico, username, password);
    UserDAO dao = UserDAO.createDAO();
    id = dao.Save(dto);
  }

  public void Update() throws SQLException {
    UserDTO dto = new UserDTO(id, -1, -1, username, null);
    UserDAO dao = UserDAO.createDAO();
    dao.Update(dto);
  }

  
  //TODO: usado para el cambio de conrasenia en capa gui, mantiene al mismo usuario pero le cambia su contrasenia en la BD
  public void ChangePasswd(String newPasswd) throws SQLException {
    UserDAO dao = UserDAO.createDAO();
    dao.EditString(id, "password", password = InputHelper.SHA512(newPasswd));
  }

  public static ArrayList<UserDTO> All() throws SQLException {
    UserDAO dao = UserDAO.createDAO();
    return dao.All();
  }

  public static UserDTO SearchByID(int id) throws SQLException {
    UserDAO dao = UserDAO.createDAO();
    return dao.UserDTO(id);
  }

  public static UserDTO Search(String username, String password) throws SQLException {
    UserDAO dao = UserDAO.createDAO();
    return dao.Search(username, InputHelper.SHA512(password));
  }

  public static ArrayList<UserDTO> SearchByUsername(String name) throws SQLException {
    UserDAO dao = UserDAO.createDAO();
    return dao.SearchByUsername(name);
  }

  public static ArrayList<UserDTO> SearchByFullUsername(String name) throws SQLException {
    UserDAO dao = UserDAO.createDAO();
    return dao.SearchByFullUsername(name);
  }

  public static ArrayList<UserDTO> SearchByMedicoID(int idMedico) throws SQLException {
    UserDAO dao = UserDAO.createDAO();
    return dao.SearchByMedicoID(idMedico);
  }

  public boolean IsRoot() {
    return gid == 0;
  }

  public boolean IsRegular() {
    return gid == REGULAR;
  }

  public boolean IsMedico() {
    return gid == MEDICO;
  }

  public int getId() {
    return id;
  }

  public int getGid() {
    return gid;
  }

  public int getIdMedico() {
    return idMedico;
  }

  public String getUsername() {
    return username;
  }

  public String getPassword() {
    return password;
  }

}
